# -*- coding: UTF-8 -*-
# __author__ = 'Sengo'

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from data_service import read_data


def init():
    glClearColor(1.0, 1.0, 1.0, 0.0)
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(0, 0, 0)
    glLoadIdentity()
    glFlush()


def draw_data():
    # init data for camera
    C = [4, 6, 10]  # Camera position
    P = [0, 0, 0]

    V = [0, 1, 0]  # Y-direction of camera

    glClearColor(1.0, 1.0, 1.0, 0.0)
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()
    gluLookAt(C[0], C[1], C[2], P[0], P[1], P[2], V[0], V[1], V[2])

    vertexs, lines = read_data()

    glColor3f(0, 0, 0)
    glPointSize(5.0)
    # vertexs
    glBegin(GL_POINTS)
    for v in vertexs:
        glVertex3f(v[0], v[1], v[2])
    glEnd()
    glFlush()

    # lines
    for line in lines:
        ves = []
        for l in line:  # 3 vertexes in a line
            ves.append(vertexs[l])
        glColor3f(0.0, 1.0, 0.0)
        glLineWidth(2.0)
        glBegin(GL_LINE_LOOP)
        for v in ves:
            glVertex3f(v[0], v[1], v[2])
        glEnd()
    glFlush()


def threeD(w, h):
    print "width:", w, "height:", h
    glViewport(0, 0, w, h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glFrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 10.0)
    glMatrixMode(GL_MODELVIEW)


if __name__ == '__main__':
    # 初始化GLU library
    # 对窗口的大小进行初始化
    glutInitWindowSize(800, 800)
    # 设置窗口出现的位置
    # glutInitWindowPosition(int

    # 初始化程序展示模式
    glutInitDisplayMode(GLUT_RGBA)
    glutCreateWindow("project of openGL")
    # 为当前窗口设置函数回调，用于画图
    glutDisplayFunc(draw_data)
    # 窗口改变时的函数
    glutReshapeFunc(threeD)
    glutMainLoop()


